package com.guiji.template.controller;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.guiji.common.annotation.RepeatSubmit;
import com.guiji.common.annotation.Log;
import com.guiji.common.core.controller.BaseController;
import com.guiji.common.core.domain.AjaxResult;
import com.guiji.common.enums.BusinessType;
import com.guiji.template.vo.TemplateFieldVo;
import com.guiji.template.bo.TemplateFieldQueryBo;
import com.guiji.template.bo.TemplateFieldAddBo;
import com.guiji.template.bo.TemplateFieldEditBo;
import com.guiji.template.service.ITemplateFieldService;
import com.guiji.common.utils.poi.ExcelUtil;
import com.guiji.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 模板字段Controller
 *
 * @author 钟维澎
 * @date 2021-07-01
 */
@Api(value = "模板字段控制器", tags = {"模板字段管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/template/templateField")
public class TemplateFieldController extends BaseController {

	private final ITemplateFieldService iTemplateFieldService;

	/**
	 * 查询模板字段列表
	 */
	@ApiOperation("查询模板字段列表")
	@PreAuthorize("@ss.hasPermi('template:templateField:list')")
	@GetMapping("/list")
	public TableDataInfo<TemplateFieldVo> list(@Validated TemplateFieldQueryBo bo) {
		return iTemplateFieldService.queryPageList(bo);
	}

	/**
	 * 导出模板字段列表
	 */
	@ApiOperation("导出模板字段列表")
	@PreAuthorize("@ss.hasPermi('template:templateField:export')")
	@Log(title = "模板字段", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult<TemplateFieldVo> export(@Validated TemplateFieldQueryBo bo) {
		List<TemplateFieldVo> list = iTemplateFieldService.queryList(bo);
		ExcelUtil<TemplateFieldVo> util = new ExcelUtil<TemplateFieldVo>(TemplateFieldVo.class);
		return util.exportExcel(list, "模板字段");
	}

	/**
	 * 获取模板字段详细信息
	 */
	@ApiOperation("获取模板字段详细信息")
	@PreAuthorize("@ss.hasPermi('template:templateField:query')")
	@GetMapping("/{id}")
	public AjaxResult<TemplateFieldVo> getInfo(@NotNull(message = "主键不能为空")
											   @PathVariable("id") Long id) {
		return AjaxResult.success(iTemplateFieldService.queryById(id));
	}

	/**
	 * 新增模板字段
	 */
	@ApiOperation("新增模板字段")
	@PreAuthorize("@ss.hasPermi('template:templateField:add')")
	@Log(title = "模板字段", businessType = BusinessType.INSERT)
	@RepeatSubmit
	@PostMapping()
	public AjaxResult<Void> add(@Validated @RequestBody TemplateFieldAddBo bo) {
		return toAjax(iTemplateFieldService.insertByAddBo(bo) ? 1 : 0);
	}

	/**
	 * 修改模板字段
	 */
	@ApiOperation("修改模板字段")
	@PreAuthorize("@ss.hasPermi('template:templateField:edit')")
	@Log(title = "模板字段", businessType = BusinessType.UPDATE)
	@RepeatSubmit
	@PutMapping()
	public AjaxResult<Void> edit(@Validated @RequestBody TemplateFieldEditBo bo) {
		return toAjax(iTemplateFieldService.updateByEditBo(bo) ? 1 : 0);
	}

	/**
	 * 删除模板字段
	 */
	@ApiOperation("删除模板字段")
	@PreAuthorize("@ss.hasPermi('template:templateField:remove')")
	@Log(title = "模板字段" , businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
								   @PathVariable Long[] ids) {
		return toAjax(iTemplateFieldService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
	}
}
